package cn.wd;

import cn.wd.common.vo.R;
import cn.wd.model.ServiceException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.http.converter.HttpMessageConversionException;
import org.springframework.web.bind.MissingServletRequestParameterException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.method.annotation.MethodArgumentTypeMismatchException;
import org.springframework.web.servlet.NoHandlerFoundException;
import org.springframework.web.bind.annotation.RestControllerAdvice;


/**
 * @author wangdong
 * @date 2024/9/13 15:57
 * @Description: 全局一场处理器
 **/

@RestControllerAdvice
public class GlobalExceptionHandler {

    private static final Logger log = LoggerFactory.getLogger(GlobalExceptionHandler.class);

    /**
     * 404找不到异常处理
     */
    @ExceptionHandler(NoHandlerFoundException.class)
    @ResponseStatus(HttpStatus.NOT_FOUND)
    public R<?> NotFoundHandler(NoHandlerFoundException e) {
        return R.FAIL(e.getClass().getSimpleName());
    }

    /**
     * 缺乏请求参数处理
     */
    @ExceptionHandler(MissingServletRequestParameterException.class)
    public R<?> processRequestParameterException(HttpServletRequest request, MissingServletRequestParameterException e) {
        print(request, e);
        String message = "缺失请求参数" + e.getParameterName();
        return R.FAIL(message);
    }

    /**
     * 请求参数类型错误处理
     */
    @ExceptionHandler(MethodArgumentTypeMismatchException.class)
    public R<?> handleMethodArgumentTypeMismatchException(MethodArgumentTypeMismatchException e, HttpServletRequest request) {
        print(request, e);
        String message = "请求参数" + e.getName() + "类型错误";
        return R.FAIL(message);
    }

    /**
     * 参数类型错误异常类型处理
     */
    @ExceptionHandler(HttpMessageConversionException.class)
    public R<?> handleHttpMessageNotReadableException(HttpMessageConversionException e, HttpServletRequest request) {
        print(request, e);
        String message = "参数类型错误";
        return R.FAIL(message);
    }

    /**
     * 空指针异常处理
     */
    @ExceptionHandler(NullPointerException.class)
    public R<?> handleNullPointerException(NullPointerException e, HttpServletRequest request) {
        print(request, e);
        String message = "空指针异常";
        return R.FAIL(message);
    }

    @ExceptionHandler
    public R<?> handleServiceException(ServiceException e) {
        return R.FAIL(e.getMessage());
    }

    /**
     * 系统异常
     */
    @ExceptionHandler(Exception.class)
    public R<?> processDefaultException(HttpServletRequest request, Exception e) {
        print(request, e);
        return R.FAIL("系统繁忙，请稍后再试");
    }

    private void print(HttpServletRequest request, Exception e) {
        log.error("请求访问路径：{}，异常信息为：{}", request.getRequestURI(), e.getMessage());
        log.error("堆栈信息", e);
    }
}
